//
//  DLInfoSearchTC.m
//  delongzhitou
//
//  Created by Mr.Wang on 2018/12/13.
//  Copyright © 2018 delongzhitou. All rights reserved.
//

#import "DLInfoSearchTC.h"
#import "DLInfoSearchModel.h"

@interface DLInfoSearchTC()


@property (nonatomic, strong) UILabel *titleLabel;

@property (nonatomic, strong) UILabel *authorLabel;

@end


@implementation DLInfoSearchTC


- (instancetype) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self layoutsubControls];
    }
    return self;
}
- (void) layoutsubControls {
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.frame = CGRectMake(10, 0, kScreenWidth - 20, 40);
    titleLabel.textColor = kDLNormalBlackColor;
    titleLabel.textAlignment = NSTextAlignmentLeft;
    titleLabel.font = kBoldFont(16);
    titleLabel.numberOfLines = 0;
    titleLabel.lineBreakMode = NSLineBreakByCharWrapping;
    [self.contentView addSubview:titleLabel];
    self.titleLabel = titleLabel;
    
    UILabel *authorLabel = [UILabel new];
    authorLabel.frame = CGRectMake(titleLabel.left, titleLabel.bottom, titleLabel.width, titleLabel.height);
    authorLabel.textColor = kColorRGB(204, 204, 204);
    authorLabel.textAlignment = NSTextAlignmentLeft;
    authorLabel.font = kSystemFont(13);
    [self.contentView addSubview:authorLabel];
    self.authorLabel = authorLabel;
    
}

- (void) setSearchModel:(DLInfoSearchModel *)searchModel {
    _searchModel = searchModel;
    
    self.authorLabel.text = searchModel.source;
    
    if (searchModel.title.length > searchModel.keyword.length) {
        NSMutableArray *attrRangeMutArr = [self getRangesWithLocalString:searchModel.title andTargetString:searchModel.keyword];
        
        NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:searchModel.title];
        
        for (NSInteger i = 0; i < attrRangeMutArr.count; i++) {
            NSValue *rangeOfValue = [attrRangeMutArr objectAtIndex:i];
            NSRange range = [rangeOfValue rangeValue];
            
            [attrStr addAttribute:NSForegroundColorAttributeName
                            value:[UIColor orangeColor]
                            range:range];
        }
        
        self.titleLabel.attributedText = attrStr;
    } else {
        self.titleLabel.text = searchModel.title;
    }
    
}

- (NSMutableArray *) getRangesWithLocalString:(NSString *)longString andTargetString:(NSString *)shortString {
    
    longString = [longString lowercaseString];
    shortString = [shortString lowercaseString];
    
    NSMutableArray *rangeArray = [NSMutableArray array];
    NSRange targetRange = NSMakeRange(0, longString.length);
    
    
    NSString *tagValue = shortString;
    NSRange range = targetRange;
    while (true) {
        range = [longString rangeOfString:tagValue options:NSLiteralSearch range:targetRange];
        if (range.location != NSNotFound) {
            [rangeArray addObject:[NSValue valueWithRange:range]];
            targetRange = NSMakeRange(NSMaxRange(range), longString.length-NSMaxRange(range));
        } else {
            break;
        }
    }
    return rangeArray;
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
